package com.bird.extension.reflect;

import com.bird.domain.dto.User;

/**
 * @Author: sijunqiang
 * @Date: 2020/4/4 14:47
 * @Description: reflect
 */
public class ReflectTarget {

    //---------------构造函数-------------------------

    /**
     * 带有参数的默认构造函数
     */
    ReflectTarget(String str) {
        System.out.println("默认的构造函数" + str);
    }

    /**
     * 无参的构造函数
     */
    public ReflectTarget() {
        System.out.println("调用了公有的无参构造方法。。。。。");
    }

    /**
     * 带有参数的构造方法
     */
    public ReflectTarget(char name) {
        System.out.println("调用带有一个参数的构造方法, 参数值" + name);
    }

    /**
     * 创建带有多个参数的构造方法
     */
    public ReflectTarget(String name, int index) {
        System.out.println("调用了带有多个参数的构造方法参数值：【目标名】：" + name + "【序号】：" + index);
    }

    /**
     * 定义一个受保护的构造函数
     */
    protected ReflectTarget(boolean n) {
        System.out.println("受保护的构造方法n:" + n);
    }

    /**
     * 私有的构造方法
     */
    private ReflectTarget(int index) {
        System.out.println("私有的构造方法 序号：" + index);
    }


    /**************************field  property**************************/

    //define four  different  access rights

    public String name;
    protected int index;
    char type;
    private String targetInfo;

    private User user;

    @Override
    public String toString() {
        return "ReflectTarget [name=" + name + ", index=" + index + ", type=" + type
                + ", targetInfo=" + targetInfo + "]";
    }


    /****************************members of the method ********************************/


    public void show1(String s) {
        System.out.println("invoke the public with  parameters method , show1() param:" + s);
    }

    protected void show2() {
        System.out.println("invoke the protected  no arguments method  show2()");
    }

    void show3() {
        System.out.println("invoke the default method ,method name show3()");
    }

    private String show4(int index) {
        System.out.println("invoke the private method  and with return value. Method name show4() param:" + index);
        return "show4()result";
    }


    /**
     * the method to get Class object
     */
    public static void main(String[] args) throws ClassNotFoundException {

        //the first way is to get the class object
        ReflectTarget reflectTarget = new ReflectTarget();
        Class reflectTargetClass1 = reflectTarget.getClass();
        System.out.println("1st:" + reflectTargetClass1.getName());

        //the second way is to get the class object
        Class reflectTargetClass2 = ReflectTarget.class;
        System.out.println("2nd:" + reflectTargetClass2.getName());

        //to see if the first one is equal to the second one.  "=="  the comparison is the address of memory
        System.out.println(reflectTargetClass1 == reflectTargetClass2);

        //the third way is to get the class object
        Class reflectTargetClass3 = Class.forName("com.bird.extension.reflect.ReflectTarget");
        System.out.println("3rd:" + reflectTargetClass3.getName());
        System.out.println(reflectTargetClass2 == reflectTargetClass3);
    }

}
